from os import pipe
import requests
from requests.api import head
import time
import re
import execjs
import random
import json
import pymysql
import base64


# 从二维码处得到session
def get_session():
    t = int(time.time()*1000)
    url = 'https://www.sf-express.com/sf-service-core-web/service/user/qrcode/getqrcode?timestamp={}'.format(
        t)
    headers1 = {
        'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Host': 'www.sf-express.com',
        'Referer': 'https://www.sf-express.com/cn/sc/dynamic_function/waybill/',
        'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
        'sec-ch-ua-mobile': '?0',
        'Sec-Fetch-Dest': 'image',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
    }
    response = requests.get(url, headers=headers1)
    session = response.headers['Set-Cookie']
    print(session)
    session = re.findall(r'SESSION=(.*?);', session)[0]
    c = response.headers['Set-Cookie'].split('; E')[0]
    return session, c


def get_before_code():
    url = 'https://inc-ubas-web.sf-express.com/json_data'
    headers = {
        'authority': 'inc-ubas-web.sf-express.com',
        'method': 'POST',
        'scheme': 'https',
        'accept': '*/*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'content-length': '563',
        'content-type': 'text/plain; charset=UTF-8',
        'origin': 'https://www.sf-express.com',
        'referer': 'https://www.sf-express.com/',
        'sec-ch-ua': '" Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"',
        'sec-ch-ua-mobile': '?0',
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67'
    }
    t = time.time()
    s_t = int(t*1000)
    t_t = time.gmtime(t)
    t_t = time.strftime("%Y-%m-%d %H:%M:%S",t_t)
    # data = "{"time":%s,"page_url":"https://www.sf-express.com/cn/sc/",\n\"page_ref\":\"\",\"page_name\":\"顺丰速运\",\n\"event_id\":106001,\n\"properties\":{\"event_name\":\"获取验证码\",\"element_name\":\"\",\"nation\":\"cn\",\"language\":\"sc\",\"phone_number\":null,\"opera_time\":\"%s\"},\n\"app_id\":\"cx\",\"app_v\":\"0.0\",\"platform\":\"pc\",\"device_id\":\"4ffc52af-5163-48d5-9265-83931946e884\",\"cookie_id\":\"\",\"sdk\":\"js\",\"sdk_name\":\"SfGather-js\",\"sdk_v\":\"1.0.14\",\"s_w\":1920,\"s_h\":1080,\"model\":\"\",\"os\":\"windows 10\",\"os_v\":\"\",\"browser\":\"chrome\",\"browser_v\":\"91.0.4472.124\",\"type\":\"event\"}" %(s_t,t_t)
    data = '{"time":%s,"page_url":"https://www.sf-express.com/cn/sc/","page_ref":"","page_name":"顺丰速运","event_id":106001,"properties":{"event_name":"获取验证码","element_name":"","nation":"cn","language":"sc","phone_number":null,"opera_time":"%s"},"app_id":"cx","app_v":"0.0","platform":"pc","device_id":"a130d3a7-690b-4eb0-89fa-5a7f474e45e6","cookie_id":"","sdk":"js","sdk_name":"SfGather-js","sdk_v":"1.0.14","s_w":1920,"s_h":1080,"model":"","os":"windows 10","os_v":"","browser":"chrome","browser_v":"88.0.4324.150","type":"event"}' %(s_t,t_t)
    data = data.encode('utf-8')
    print(data)
    res = requests.post(url,headers=headers,data=data)
    print(res.text)
    return s_t

def get_code(t,session):
    # t = t + 6
    t = int(time.time()*1000)
    url = 'https://www.sf-express.com/sf-service-owf-web/service/captcha/sms?force=true&time={}'.format(t)
    headers = {
        'Accept': 'image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Host': 'www.sf-express.com',
        'Referer': 'https://www.sf-express.com/cn/sc/',
        'sec-ch-ua': '" Not;A Brand";v="99", "Microsoft Edge";v="91", "Chromium";v="91"',
        'sec-ch-ua-mobile': '?0',        
        'Sec-Fetch-Dest': 'image',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Site': 'same-origin',
        'Cookie': 'SESSION=%s'%session,
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67'
    }
    res = requests.get(url,headers=headers)
    with open('E:/nodeProject/crawlab/sf/sf.png', 'wb') as f:
        f.write(res.content)

def get_yzm(phone,code,session):
    headers = {
        'Host': 'www.sf-express.com',
        'sec-ch-ua': '',
        'Accept': '*/*',
        'X-Requested-With': 'XMLHttpRequest',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.67',
        'Content-Type': 'application/json;charset=UTF-8',
        'Origin': 'https://www.sf-express.com',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Dest': 'empty',
        'Referer': 'https://www.sf-express.com/cn/sc/',
        'Cookie': 'SESSION=%s'%session,
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    }
    data = 'lang=sc&region=cn&translate='
    url = 'https://www.sf-express.com/sf-service-owf-web/service/sms/user/19924237889/notification?app=sms&answer={}&area=&lang=sc&region=cn&translate='.format(code)
    res = requests.post(url, headers=headers, data=data)
    print(res.text)

def get_login(sms_code,phone,session):
    url = 'https://www.sf-express.com/sf-service-core-web/service/user/sms/login?rememberMe=true&lang=sc&region=cn&translate='
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
        'Connection': 'keep-alive',
        'Content-Length': '28',
        'Content-Type': 'application/json;charset=UTF-8',
        'Cookie': 'SESSION=%s'%session,
        'Host': 'www.sf-express.com',
        'isRegister': 'true',
        'Origin': 'https://www.sf-express.com',
        'Referer': 'https://www.sf-express.com/cn/sc/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'SMS-Verification-App': 'user',
        'SMS-Verification-Code': sms_code,
        'SMS-Verification-Mobile': phone,
        'SMS-Verification-Mobile-Area':'',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }
    data = 'lang=sc&region=cn&translate='
    res = requests.post(url,headers=headers,data=data)
    print(res.headers)
    cookies = res.headers['Set-Cookie']
    print(cookies)

def get_data(session,f,i):
    url = 'https://www.sf-express.com/sf-service-core-web/service/waybills/list/rec?lang=sc&region=cn&translate=&pageNo={}&pageRows=10'.format(i)
    Passwd = execjs.compile(open(r"E:/nodeProject/js/uuid.js",encoding='UTF-8').read()).call('UUID')
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
        'Cookie':'SESSION=aaadcb298c034cecb0daf647b0a2ebe6; remember-me=YmY1NzczMjk4MWEzNGI2N2JiYjcxNTI4NjNhOGIyNGY6ZDkyMDVmMjY5YjU0NDUwYjg2NGRkZjk3Y2E2YjIzNGE=;sajssdk_2015_cross_new_user=1; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22{}%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%7D%2C%22%24device_id%22%3A%22{}%22%7D'.format(Passwd,Passwd)
    }
    response = requests.get(url,headers=headers)
    print(response.text)
    result = response.json()
    for data in result['result']['content']:
        print(data)
        f.write(str(data)+'\n')


def get_detail_data(mailno, session, cookies, dict1):
    url = 'https://www.sf-express.com/sf-service-core-web/service/waybill/{}/electronicStub?lang=sc&region=cn&translate=&securityExpress=false'.format(
        mailno)
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
        'Cookie': 'SESSION='+session+';'+cookies
    }
    res = requests.get(url, headers=headers)
    result = res.json()['result']
    print('======================================================')
    print(result)
    try:
        dict1['日期'] = result['consignedTm']
        # if str(dict1['日期'])[0:10] != '2021-10-05':
        #     print('日期不对应')
        #     return
        dict1['寄件号码'] = result['consignorPhone']
        dict1['寄件地址'] = result['consignorAddr']
        dict1['收件号码'] = result['addresseePhone']
        dict1['收件地址'] = result['addresseeAddr']
        dict1['寄件物品'] = result['innerWaybillConsigns']
        dict1['实重'] = result['realWeightQty']
        dict1['计重'] = result['meterageWeightQty']
        if result['feeList'][0]['feeName'] == '运费':
            dict1['运费'] = result['feeList'][0]['feeAmt']
        else:
            dict1['运费'] = result['feeList'][2]['feeAmt']
        print(dict1)
        # sql = '''insert into sf_data(mailno,sendname,sendcity,recvname,recvcity,sendaddress,recvaddress,product,realweight,mathweight,price,consignorPhone,addresseePhone,time) VALUES(%s, %s, %s, %s, %s, %s, %s, %s,%s, %s, %s, %s, %s, %s)'''
        # values = (dict1['快递单号'], dict1['寄件人'], dict1['寄件城市'], dict1['收件人'], dict1['收件城市'],
        #           dict1['寄件地址'], dict1['收件地址'], dict1['寄件物品'], dict1['实重'], dict1['计重'], dict1['运费'], dict1['寄件号码'], dict1['收件号码'], dict1['日期'])
        # cur.execute(sql, values)
        # connets.commit()
    except Exception as e:
        print('出现错误啦', e)


def getsearch():
    '''
    websig:"b07e4056b2e0dcc3ffaa871f29d21d9bc88e942f8b66e379a0176b5046fe2d2eaf7a243971e3fb2b52f8bda4aa42eba4c88ca2a2fe3f17515b25ee88d7377530"
    asig:"hPYCsQQPPHwwqO-5jRZFKnPj_EuBEHPuDyX5c3M7jB_wnieDTtNRQBzEKEcWbv7b4QvVQwhEZVxQJwMrd8ZYJjPRHhZfGKwO",
    ua:"TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzkyLjAuNDUxNS4xMDcgU2FmYXJpLzUzNy4zNg==",
    sess:"2YM-8z44EpECwi1vb3Ea0vo0mH603Pj8_Wdnn0YFsCSfJfRswX_VK7n6wsY28alDSCSJ-CmhMoYfMIktEwax2Pt_UmPxqaLqsJHAW9o_WyHvcOziw1lHpnEy8BqlV-kww7yFQWh286U7rbX6KCGtL1VI0wKc_j-JsSGtmB-hUF3CJXJn2XI-EX3Uc8_aJJmzLOdgpWnsaVA*",
    vsig:变化
    '''
    url = "https://www.sf-express.com/sf-service-owf-web/service/captcha/getJsUrl?lang=sc&region=cn&translate="
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest"
    }
    res = requests.get(url,headers=headers)
    print(res.text)
    asig = res.text[-96:]
    print(asig)
    rand = random.random()
    url = 'https://captcha.guard.qcloud.com/cap_union_prehandle?aid=1252000277&asig=%s&captype=&protocol=https&clientype=2&disturblevel=&apptype=&curenv=open&ua=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzkyLjAuNDUxNS4xMDcgU2FmYXJpLzUzNy4zNg==&uid=&cap_cd=&lang=2052&random=%s&callback=_aq_390201&subsid=1'%(asig,rand)
    res = requests.get(url,headers=headers)
    res = json.loads(res.text.replace("_aq_390201(","").replace(")",""))
    sess = res["sess"]
    print(sess)
    # asig = re.findall('asig=(.*?)',res.text,re.M)
    createIframeStart = str(int(time.time()*1000))
    url = 'https://captcha.guard.qcloud.com/cap_union_new_show?aid=1252000277&asig=%s&captype=&protocol=https&clientype=2&disturblevel=&apptype=&curenv=open&ua=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzkyLjAuNDUxNS4xMDcgU2FmYXJpLzUzNy4zNg==&sess=%s&theme=&sid=6992849415598566977&showtype=popup&fb=1&forcestyle=undefined&subsid=2&color=000000&uid=&cap_cd=&lang=2052&rnd=446664&TCapIframeLoadTime=35&prehandleLoadTime=50&createIframeStart=%s'%(asig,sess,createIframeStart)
    # url = "https://captcha.guard.qcloud.com/cap_union_new_getsig?aid=1252000277&asig=pVB_2pBBwk19U-wP16Wdm1m2YYO89NzESHLjQ6LbbD09QSJxs60nmnuBkkvCWejaLr-yXCsecpowz9Si3G7ZxpZboUBuNx2C&captype=&protocol=https&clientype=2&disturblevel=&apptype=&curenv=open&ua=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzkyLjAuNDUxNS4xMDcgU2FmYXJpLzUzNy4zNg==&sess=HyZtvXHZWakxgDY6vcBECCdN7MPih2C_hr4zfFe2TcyijlyVIUV1C5nYpEbPEK6eimfsrf5WSXk1aoQC9DctcJL3muRlBfHo8IwRBSg4al2wBrTa2kezX5jTdcvhM_7rA6AJJKRkyjWqDZkKoC-KP6ZTJCuWAawOiC195A6Ejo52izsCj_ucvH1zBNhSJ97cysx6B5bdWCs*&theme=&sid=6992849415598566977&showtype=popup&fb=1&forcestyle=undefined&subsid=6&color=000000&uid=&cap_cd=&lang=2052&rnd=446664&TCapIframeLoadTime=35&prehandleLoadTime=50&createIframeStart=%s&rand=%s"%(createIframeStart,str(rand))
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
        "Cookie": "qcloud_from=qcloud.baidu.seo-1627441805991",
        "Host": "captcha.guard.qcloud.com"
    }
    res = requests.get(url,headers=headers)
    websig = re.findall('websig:"(.*?)",',res.text)[0]
    vsig = re.findall(',tt="(.*?)",et=',res.text)[0]
    print(websig,vsig)
    rand = random.random()
    t = str(int(time.time()*1000))
    url1 = "https://captcha.guard.qcloud.com/cap_union_new_getcapbysig?aid=1252000277&asig=%s&captype=&protocol=https&clientype=2&disturblevel=&apptype=&curenv=open&ua=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzkyLjAuNDUxNS4xMDcgU2FmYXJpLzUzNy4zNg==&sess=%s&theme=&sid=6992849415598566977&showtype=popup&fb=1&forcestyle=undefined&subsid=3&color=000000&uid=&cap_cd=&lang=2052&rnd=446664&TCapIframeLoadTime=35&prehandleLoadTime=50&createIframeStart=%s&rand=%s&websig=%s&vsig=%s&img_index=1"%(asig,sess,t,rand,websig,vsig)
    url2 = "https://captcha.guard.qcloud.com/cap_union_new_getcapbysig?aid=1252000277&asig=%s&captype=&protocol=https&clientype=2&disturblevel=&apptype=&curenv=open&ua=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzkyLjAuNDUxNS4xMDcgU2FmYXJpLzUzNy4zNg==&sess=%s&theme=&sid=6992849415598566977&showtype=popup&fb=1&forcestyle=undefined&subsid=4&color=000000&uid=&cap_cd=&lang=2052&rnd=446664&TCapIframeLoadTime=35&prehandleLoadTime=50&createIframeStart=%s&rand=%s&websig=%s&vsig=%s&img_index=2"%(asig,sess,t,rand,websig,vsig)
    print(url1)
    print(url2)
    res = requests.get(url1,headers=headers)
    with open("E:/nodeProject/python/sf_yzm1.png", "wb") as f:
        f.write(res.content)
    res = requests.get(url2,headers=headers)
    with open("E:/nodeProject/python/sf_yzm2.png", "wb") as f:
        f.write(res.content)


def slide(distance):
    list1 = []
    list1[0] = [56,288,5]
    x = 0
    y = 288
    t = random.randint(7,10)
    one_distance = int(distance / 2)  # 在一半距离的时候开始减速
    two_distance = one_distance + int(distance/5) # 减速的距离为总距离的五分之一
    sl = 0
    r = random.randint(7,10)
    # 第一次
    while sl < one_distance:
        if sl == r:
            x = x + 2
            r = r + random.randint(7,10)
        else:
            x = x + 1
    while one_distance < sl < two_distance:
        if sl == r:
            x = x + 2
            r = r + random.randint(7,10)
        else:
            x = x + 1
    while sl > two_distance:
        if sl == r:
            x = x + 2
            r = r + random.randint(7,10)
        else:
            x = x + 1


# 第三方识别
def base64_api(uname, pwd, img, typeid):
    with open(img, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        b64 = base64_data.decode()
    data = {"username": uname, "password": pwd,
            "typeid": typeid, "image": b64}
    result = json.loads(requests.post(
        "http://api.ttshitu.com/predict", json=data).text)
    if result['success']:
        return result["data"]["result"]
    else:
        return result["message"]
    return ""



if __name__ == "__main__":
    connets = pymysql.connect(host='192.168.0.14', port=3306,
                                 user='junfeng', passwd='Gzjunfeng888!', db='jf_python_db')
    cur = connets.cursor()
    connet = pymysql.connect(host='39.108.165.120', port=3306, user='jf_db_center',
                             passwd='x1hP#P1QiTTF1bFby$NCcfQlaWx9H7vE', db='jf_datacenter')
    cursor = connet.cursor()
    # session = get_session()
    # print(session)
    # t = get_before_code()
    # get_code(t,session)
    # code = input('请输入图形验证码:')
    # phone = "19924237889"
    # get_yzm(phone,code,session)
    # sms_code = input('请输入短信验证码:')
    # get_login(sms_code,phone,session)
    # f = open('sf_data_2482.txt','a+',encoding='utf-8')
    # session = "1e69b717aa1a4c73b348eb706de1a67c"
    # for i in range(3787,4925):
    #     print(i)
    #     get_data(session,f,i)
    #     time.sleep(1.5)
    session, c = get_session()
    print(session)
    t, cc = get_before_code()
    print(c)
    get_code(t, session, c)
    code = base64_api(uname='Junfeng', pwd='.jf.888',
                      img='E:/nodeProject/crawlab/sf/sf.png', typeid=1003)
    print(code)
    phone = "19924237889"
    get_yzm(phone, code, session, c)
    sql = '''select code from df_tasker_sms_data where datatime > 2021-09-26 and courier = "顺丰速运"'''
    time.sleep(10)
    cursor.execute(sql)
    sms_code = cursor.fetchall()[-1][0]
    print(sms_code)
    cookies = get_login(sms_code, phone, session, c)
    # getsearch()
    dict1 = {}
    get_detail_data('SF1402322012129', session, c, dict1)
